package com.somsoft.jtgj;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

public class LearnActivity extends Activity implements OnClickListener{

	private int currId = 0;
	private ImageButton btnLeft;
	private ImageButton btnRight;
	private ImageButton btnTopCenter;
	private ImageButton btnBottomCenter;
	
	private RelativeLayout learnLayout;
	
	public List<Integer> pics = new ArrayList<Integer>();
	public List<Integer> sounds = new ArrayList<Integer>();
	
	private SoundPool sp;
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        MyApplication.getInstance().addActivity(this);
	        //全屏显示
	        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        //无标题
	        requestWindowFeature(Window.FEATURE_NO_TITLE);
	        
	        setContentView(R.layout.learn);
	        learnLayout = (RelativeLayout)findViewById(R.id.learnLayout); 
	        btnLeft = (ImageButton)findViewById(R.id.btnLeft); 
	        btnRight = (ImageButton)findViewById(R.id.btnRight); 
	        btnTopCenter = (ImageButton)findViewById(R.id.btnTopCenter); 
	        btnBottomCenter = (ImageButton)findViewById(R.id.btnBottomCenter); 
	        
	        btnLeft.setOnClickListener(this);
	        btnRight.setOnClickListener(this);
	        btnTopCenter.setOnClickListener(this);
	        btnBottomCenter.setOnClickListener(this);
	        
	      //实例SoundPool播放器
			sp = new SoundPool(20, AudioManager.STREAM_MUSIC, 100);
			
	        for(int i=0;i<17;i++){
				pics.add(R.drawable.p00 + i);
				sounds.add(sp.load(this, R.raw.p00 + i, 1));
	        }
	        sp.play(sounds.get(currId), 2, 2, 0, 0, 1);
	 }

	 
//	@Override
//	public void onWindowFocusChanged(boolean hasFocus) {
//		if (hasFocus) {
//			sp.play(sounds.get(currId), 2, 2, 0, 0, 1);
//		}
//	}
	 
	@Override
	public void onClick(View v) {
		if (v == btnLeft) {
			currId--;
			if (currId < 0) {
				currId = pics.size() - 1;
			}
			learnLayout.setBackgroundResource(pics.get(currId));
			sp.play(sounds.get(currId), 2, 2, 0, 0, 1);
		} else if (v == btnRight) {
			currId++;
			if (currId >= pics.size()) {
				currId = 0;
			}
			learnLayout.setBackgroundResource(pics.get(currId));
			sp.play(sounds.get(currId), 2, 2, 0, 0, 1);
		} else if(v == btnTopCenter){
			confirm();
		} else if(v == btnBottomCenter){
			sp.play(sounds.get(currId), 2, 2, 0, 0, 1);
		}
	}
	
	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
			confirm();
			return true;
		}
		return super.onKeyUp(keyCode, event);
	}
	
	
	private void confirm(){
		Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("确认返回主界面吗？");  
		builder.setTitle("提示");  
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
				Intent it = new Intent(LearnActivity.this,MainActivity.class);
				startActivity(it);
			}
		});
		builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}
	
}





















